package my.jbf.firstGame.GAMEDATA;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import my.jbf.firstGame.MODULE.RandomID;
import my.jbf.firstGame.R;

import android.util.*;


public class UserDBManager {
    private static UserDBManager instance = null;
    private final UserDBHelper mUserDBHelp;
    private final SQLiteDatabase mSQLiteDB;
    private final Context mContext;


    public static UserDBManager getInstance(Context context) {
        if (instance == null) {
            instance = new UserDBManager(context);


        }

        return instance;
    }

    private UserDBManager(Context context) {
        mContext=context;
        mUserDBHelp = new UserDBHelper(context,context.getExternalCacheDir().getPath()+mContext.getString(R.string.OutSideUserDBAdress));
        mSQLiteDB = mUserDBHelp.getWritableDatabase();
        

    }



    public String getUserID(String name,String psw) {//根据用户账号获取用户的游戏ID
        Cursor mCursor;
        mCursor=mSQLiteDB.rawQuery("select * from "+mContext.getString(R.string.TableName_UserPSW)
								   +" where "+mContext.getString(R.string.USER_PSW_UserName)+" = ?"
								   +" and "+mContext.getString(R.string.USER_PSW_Password)+" = ?",new String[]{name,psw} );
        Log.d("jbf","0000length="+mCursor.getCount());
        if(mCursor.getCount()>0)
	   {
		   Log.d("jbf","length="+mCursor.getCount());
		   mCursor.moveToNext();
		   String bac=mCursor.getString(mCursor.getColumnIndex(mContext.getString(R.string.USER_PSW_PlayID)));
		   Log.d("jbf","length="+mCursor.getCount()+",getID="+bac);
		   return bac;
	   }
        return null;

    }

    public void createUser(String name, String PSW)
    {//增加可以登录游戏的账号密码，并生成唯一25位ID
		ContentValues mCV = new ContentValues();
        mCV.put(mContext.getString(R.string.USER_PSW_UserName), name);
        mCV.put(mContext.getString(R.string.USER_PSW_Password), PSW);
        mCV.put(mContext.getString(R.string.USER_PSW_CreateTime),System.currentTimeMillis());
        String randomID;
        do {
            randomID = (new RandomID().getRandomID("USER", 25));
            }while(isUserIDRepetition(randomID));

        mCV.put(mContext.getString(R.string.USER_PSW_PlayID), randomID);
		Log.d("jbf","add user="+name+",psw="+PSW+",ID="+randomID);
        mSQLiteDB.insert(mContext.getString(R.string.TableName_UserPSW),null,mCV);
		
    }
    public boolean isUserNameRepetition(String str)
    {//查看用户名是否重复
         Cursor mCursor;
        mCursor=mSQLiteDB.rawQuery("select * from "+mContext.getString(R.string.TableName_UserPSW)+" where "+mContext.getString(R.string.USER_PSW_UserName)+" =?",new String[]{str});
        while(mCursor.moveToNext())
        {
            return true;
        }
        return false;
    }
    public boolean isUserIDRepetition(String str)
    {//查询用户ID是否重复
         Cursor mCursor;
        mCursor=mSQLiteDB.rawQuery("select * from "+mContext.getString(R.string.TableName_UserPSW)+" where "+mContext.getString(R.string.USER_PSW_UserName)+" =?",new String[]{str});
        while(mCursor.moveToNext())
        {
            return true;
        }
        return false;

    }
	public boolean isPSWRepetition(String name, String psw)
	{ //确定用户输入的账号密码是否与数据库中匹配
		Cursor mCursor;
        mCursor=mSQLiteDB.rawQuery("select * from "+mContext.getString(R.string.TableName_UserPSW)
                +" where "+mContext.getString(R.string.USER_PSW_UserName)+" = ?"
                +" and "+mContext.getString(R.string.USER_PSW_Password)+" = ?",new String[]{name,psw} );
        while(mCursor.moveToNext())
        {
            mCursor.close();
            return true;
        }
        mCursor.close();
        return false;

	}
	public Cursor getUserPSWandID(String name)
    {//查询用户ID，返回查询结果
        Cursor mCursor;
        mCursor=mSQLiteDB.rawQuery("select * from "+mContext.getString(R.string.TableName_UserPSW)
                +" where "+mContext.getString(R.string.USER_PSW_Password)+" = ?",new String[]{name} );
        return  mCursor;
    }
}
